//! Serialization format example
//!
//! This example demonstrates how to use different serialization formats with Kameo.
//! Run with:
//!
//! ```bash
//! cargo run --example serialization_formats --features "remote json"
//! ```

// We need to enable the remote feature to access serialization
#[cfg(feature = "remote")]
mod example {
    use kameo::serialization::{self, SerializationConfig, SerializationFormat};
    use serde::{Deserialize, Serialize};
    
    // Only include JSON format if the json feature is enabled
    #[cfg(feature = "json")]
    
    #[derive(Debug, Serialize, Deserialize)]
    struct Person {
        name: String,
        age: u32,
    }
    
    pub fn run() {
        println!("Serialization formats example\n");
        
        let person = Person {
            name: "Alice".to_string(),
            age: 30,
        };
        
        // Default serialization (MessagePack)
        let messagepack_bytes = serialization::serialize(&person)
            .expect("Failed to serialize with MessagePack");
        
        println!("MessagePack serialization:");
        println!("- Serialized size: {} bytes", messagepack_bytes.len());
        println!("- Hex: {}", hex_string(&messagepack_bytes));
        
        let deserialized_person: Person = serialization::deserialize(&messagepack_bytes)
            .expect("Failed to deserialize with MessagePack");
        
        println!("- Deserialized: {:?}", deserialized_person);
        
        // JSON serialization (only if the json feature is enabled)
        #[cfg(feature = "json")]
        {
            // Configure serialization to use JSON
            let json_config = SerializationConfig::new().with_format(SerializationFormat::Json);
            serialization::init_serialization(json_config);
            
            let json_bytes = serialization::serialize(&person)
                .expect("Failed to serialize with JSON");
            
            println!("\nJSON serialization:");
            println!("- Serialized size: {} bytes", json_bytes.len());
            println!("- JSON: {}", String::from_utf8_lossy(&json_bytes));
            
            let deserialized_person: Person = serialization::deserialize(&json_bytes)
                .expect("Failed to deserialize with JSON");
            
            println!("- Deserialized: {:?}", deserialized_person);
            
            // Reset to MessagePack for comparison
            let messagepack_config = SerializationConfig::new().with_format(SerializationFormat::MessagePack);
            serialization::init_serialization(messagepack_config);
            
            println!("\nComparison:");
            println!("- MessagePack size: {} bytes", messagepack_bytes.len());
            println!("- JSON size: {} bytes", json_bytes.len());
            println!("- Size ratio: {:.2}x", json_bytes.len() as f64 / messagepack_bytes.len() as f64);
        }
    }
    
    // Helper function to display binary data as hex
    fn hex_string(bytes: &[u8]) -> String {
        bytes.iter()
            .map(|b| format!("{:02x}", b))
            .collect::<Vec<_>>()
            .join("")
    }
}

// Main entry point
fn main() {
    #[cfg(feature = "remote")]
    example::run();
    
    #[cfg(not(feature = "remote"))]
    println!("This example requires the 'remote' feature. Run with:\ncargo run --example serialization_formats --features remote");
} 